Ext.require([
    'Ext.form.field.ComboBox',
    'Ext.form.FieldSet',
    'Ext.tip.QuickTipManager',
    'Ext.toolbar.Paging',
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.util.*',
    'Ext.state.*'
]);

Ext.onReady(function() {
    //结算方式
    var statementTypeList = [
        {"value":"1","text":"现金"},
        {"value":"4","text":"转账"},
        {"value":"5","text":"POS机"},
        {"value":"10","text":"折扣"}
    ];
    var statementTypeStore = Ext.data.Store({
        autoDestroy: true,
        fields :["value","text"],
        data : statementTypeList
    });

    //入账银行
    var bankList = [
        {"value":"1","text":"华融湘江银行株洲芦淞支行预售资金监管户"},
        {"value":"2","text":"建设银行株洲人民路支行"},
        {"value":"3","text":"建设银行株洲人民路支行0165"},
        {"value":"4","text":"浦发银行监管户"},
        {"value":"5","text":"浦发银行株洲长江路支行"},
        {"value":"6","text":"浦发银行株洲长江路支行0409"},
        {"value":"7","text":"浦发银行株洲长江路支行保证金户"},
        {"value":"8","text":"兴业银行株洲石峰支行"},
        {"value":"9","text":"招商银行株洲支行"},
        {"value":"10","text":"招商银行株洲支行预售资金监管专户"},
        {"value":"11","text":"中国工商银行株洲高新技术开发支行"},
        {"value":"12","text":"中国建设银行株洲电力支行预售资金监管户"},
        {"value":"13","text":"中国银行株洲清水塘支行"},
        {"value":"14","text":"中信银行株洲建设路支行"},
        {"value":"15","text":"华融湘江银行株洲芦淞支行"}
    ];
    var bankStore = Ext.data.Store({
        autoDestroy: true,
        fields :["value","text"],
        data : bankList
    });
    
    //员工list
    var userList = new Ext.data.Store({
        singleton:true,
        autoDestroy:true,
        fields: ["userName", "id"],
        proxy:{
            type:'ajax',
            url:jypath+'/marketing/haveReceiveRefund/findUserByMerID',
            reader:{
                type: 'json', 
                root: 'root'  //数据
           }
        },
        autoLoad:true
    });
    
    //银行账号
    var getBankAccount = new Ext.data.Store({
        singleton:true,
        autoDestroy:true,
        fields: ["dictionaryName"],
        proxy:{
            type:'ajax',
            url:jypath+'/estate/dictionary/getDictionary?dictionaryType=BA',
            reader:{
                type: 'json', 
                root: 'root'  //数据
           }
        },
        autoLoad:true
    });
	
	var form_refund_value = new Ext.form.FormPanel({
		title : "收支信息",
		region:'north',
		frame : true,
		minHeight:150,
		defaults: {
			//width : 200
		},
		items:[
			{
				xtype: "container",
			    layout: "hbox",
			    items: [
			        { xtype: "displayfield", fieldLabel: "业务类型", value: relation, labelWidth:60, width:300},
			        { xtype: "displayfield", fieldLabel: "业务编号", value: relationCode, labelWidth:60, width:400}
			    ]
			},
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	                { xtype: "displayfield", fieldLabel: "客户名称", value: customer_name, labelWidth:60, width:300},
	                { xtype: "displayfield", fieldLabel: "房&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号", value: combined_name, labelWidth:60, width:400}
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	                { xtype: "displayfield", fieldLabel: "签约日期", value: dateFormat(relationDate), labelWidth:60, width:300},
					{ xtype: "displayfield", fieldLabel: "经纪人", value: agent_name, labelWidth:60}
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	                { xtype: "displayfield", fieldLabel: "签约面积", value: calculatArea, labelWidth:60, width:300},
					{ xtype: "displayfield", fieldLabel: "签约单价", value: price, labelWidth:60}
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	                { xtype: "displayfield", fieldLabel: "签约金额", value: totalPrice, labelWidth:60, width:300},
					{ xtype: "displayfield", fieldLabel: "可退金额", value: realAmount, labelWidth:60}
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	                { xtype: "displayfield", fieldLabel: "备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注", value: remark, labelWidth:60, width:300}
	            ]
		    }
		 ]
	});
	
	var gridStore_refund = Ext.create('Ext.data.Store', {
        fields: ["id", "relationCode", "relationType", "businessType", "realAmount", "billCode",
                  "statementDate", "statement_name", "paymentMethod_name", "remark"],
        //是否在服务端排序 （true的话，在客户端就不能排序）
        remoteSort: false,
        proxy: {
            type: 'ajax',
            url: jypath+'/marketing/haveReceiveRefund/refund_list_view?id='+re_id+"&needOrHave="+needOrHave,
            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'rows',  //数据
                totalProperty: 'total' //数据总条数
            }
        },
        autoLoad: true  //即时加载数据
    });
	
	var grid_refund = Ext.create('Ext.grid.Panel', {
        store: gridStore_refund,
        columnLines: true,
        region:'north',
        columns: [{
            text     : '序号',
            dataIndex: 'id'
        },{
            text     : '房款类型',
            dataIndex: 'relationType'
        },{
            text     : '业务标志',
            dataIndex: 'businessType'
        },{
            text     : '发生额',
            dataIndex: 'realAmount'
        },{
            text     : '票据号码',
            dataIndex: 'billCode'
        },{
            text     : '结算日期',
            dataIndex: 'statementDate',
            renderer : function(val) {
				if(val!=null&&val!=""){
					return dateFormat(val);
				}
				return null;
			}
        },{
            text     : '结算人员',
            dataIndex: 'statement_name'
        },{
            text     : '结算方式',
            dataIndex: 'paymentMethod_name'
        },{
            text     : '备注',
            dataIndex: 'remark'
        }],
        title: '已收款信息'
    });
	
	if(isDtk=="true"){
		grid_refund = Ext.create('Ext.grid.Panel', {
	        store: gridStore_refund,
	        columnLines: true,
	        region:'north',
	        columns: [{
	            text     : '序号',
	            dataIndex: 'id'
	        },{
	            text     : '房款类型',
	            dataIndex: 'relationType'
	        },{
	            text     : '业务标志',
	            dataIndex: 'businessType'
	        },{
	            text     : '发生额',
	            dataIndex: 'realAmount'
	        },{
	            text     : '备注',
	            dataIndex: 'remark'
	        }],
	        title: '待退款信息'
	    });
 	}
	
	var form_refund = new Ext.form.FormPanel({
		title : "退款信息",
		region : 'north',
		frame : true,
		defaults: {
			//width : 200
		},
		items:[
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
					{ 
					  xtype: "numberfield",
					  fieldLabel: "&nbsp;手&nbsp;&nbsp;续&nbsp;费",
					  id: "counterFee",
					  name: "counterFee",
					  value: 0,
					  labelWidth:60,
					  minValue: 0,
					  hideTrigger:true,
					  maxValue: Number(realAmount)
					},
					{ xtype: "numberfield", 
					  fieldLabel: "&nbsp;退款金额", 
					  id: "realAmount", 
					  name: "realAmount", 
					  value: Number(realAmount), 
					  labelAlign:'right', 
					  labelWidth:110,
					  hideTrigger:true,
					  minValue: 0
					}
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
					{  
					    xtype:'combobox',  
						id:'statementId',  
						store: userList,
						displayField: "userName",           
						valueField: "id",
						fieldLabel:'<font style="color: #e90000;">*</font>结算人员',  
						queryMode:'local',
						typeAhead: true,
						labelWidth:60,
						allowBlank: false,
						blankText: "结算人员不能为空!",
						listeners : {
					 		afterRender : function(combo) {
					 			//1、设置值的时候,必须保证与store中的值具有相同的类型
					 			//2、必须是在store加载完成之后
					         	combo.setValue(parseInt(userId));
					      	}
					   	}
					}, 
        	        {  
	                  xtype:'combobox',
	 		    	  fieldLabel:'<font style="color: #e90000;">*</font>结算方式',
	 		    	  store:statementTypeStore,
	 		    	  displayField: "text",
	 		    	  valueField:"value",
	 		    	  id:'statementType',
	 		    	  queryMode:'local',
				      typeAhead: true,
				      labelAlign:'right',
				      width:200,
				      labelWidth:110,
				      allowBlank: false,
					  blankText: "结算人员不能为空!"
        	        },
        	        {  
  	                  xtype:'combobox',
  	 		    	  fieldLabel:'',
  	 		    	  store:bankStore,
  	 		    	  displayField: "text",
  	 		    	  valueField:"value",
  	 		    	  id:'bankAccount',
  	 		    	  queryMode:'local',
  				      typeAhead: true,
  				      width:300
          	        }
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
					{
						xtype:'datefield',
					   	format:'Y-m-d',
					   	fieldLabel:'<font style="color: #e90000;">*</font>结算日期',
					   	id:'statementDate',
					   	labelWidth:60,
					   	format: 'Y-m-d',
						value: new Date(),
						minValue: new Date(),
						allowBlank: false,
					    blankText: "结算日期不能为空!"
					}, 
					{ xtype: "textfield", fieldLabel: "&nbsp;摘&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;要", id: "refundRemark", name: "refundRemark", width:500, labelAlign:'right', labelWidth:110}
	            ]
		    }
		 ],
		 dockedItems: [{
	            xtype: 'toolbar',
	            dock: 'bottom',
	            ui: 'footer',
	            layout: {
	                pack: 'left'
	            },
	            items: [{
	                minWidth: 80,
	                text: '保存',
	                id:'save_button',
	                handler:save_checked
	            },{
	                minWidth: 80,
	                text: '返回',
	                handler:backProjectDetail
	            }]
	      }]
	});
	
	var viewport = Ext.create('Ext.Viewport', {
		layout : 'border',
		items : [form_refund_value, grid_refund, form_refund]
	});
	
	function save_checked(){
		var form = form_refund.getForm();
		var newRealAmount=Ext.getCmp("realAmount").getValue();
		if(Number(newRealAmount)>Number(realAmount)){
			Ext.MessageBox.alert("系统提示","退款金额大于应退款金额("+realAmount+")");
			return;
		}
		if(Number(newRealAmount)<=0){
			Ext.MessageBox.alert("系统提示","退款金额应大于0元");
			return;
		}
	    if(form.isValid()){
		    Ext.Ajax.request({
				url: jypath+'/marketing/haveReceiveRefund/addRefund',
				params:{
					counterFee:Ext.getCmp("counterFee").getValue(),
					statementType:Ext.getCmp("statementType").getValue(),
					bankAccount:Ext.getCmp("bankAccount").getValue(),
					realAmount:newRealAmount,
					date:Ext.getCmp("statementDate").getValue(),
					statementId:Ext.getCmp("statementId").getValue(),
					refundRemark:Ext.getCmp("refundRemark").getValue(),
					refundId:re_id,
					needOrHave:needOrHave
				},
				callback: function (options, success, response) {
					 if (success) {
						 Ext.MessageBox.alert("系统提示","退款成功"); 
						 setTimeout(function(){backProjectDetail();}, 2000);
		        	 }else{
		        		 Ext.MessageBox.alert("系统提示","系统异常!");
		        	 }
				}
				
			});
	    }
	}
	
	function backProjectDetail(){
		location.href=jypath+"/marketing/needReceiveRefund/index";
	}
	
	function dateFormat(value){ 
	    if(null != value && value != ""){ 
	        return Ext.Date.format(new Date(value),'Y-m-d'); 
	    }else{ 
	        return ""; 
	    } 
	} 
	

	
});
